package cn.kawa.org.controller;

import cn.kawa.basic.exception.BusinessException;
import cn.kawa.basic.util.ExcelUtils;
import cn.kawa.basic.util.JsonResult;
import cn.kawa.basic.util.PageList;
import cn.kawa.org.domain.Shop;
import cn.kawa.org.domain.ShopLog;
import cn.kawa.org.dto.ShopDto;
import cn.kawa.org.query.ShopQuery;
import cn.kawa.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

//@Controller  //@responseBody
@RestController //扫描此controller，返回json数据
@RequestMapping("shop")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 此接口用于店铺注册邮件里点击激活，更改店铺的一个状态
     */
    @GetMapping("/active/{id}")
    public void active(@PathVariable("id")Long id,HttpServletResponse response){
        try {
            shopService.active(id);
            response.sendRedirect("http://localhost:8081/#/login");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** domain pojo 实体类 对应表格数据
     * dto : 几首参数获取传参使用
     * @param response
     */
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return  shopService.getEcharts();
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            //1.查询所有
            List<Shop> list = shopService.findAll();
            //2.调用方法
            ExcelUtils.exportExcel(list,null,"店铺信息",Shop.class,"shop.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            //1.调用工具类导入
            List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            //2.保存到数据库
            //list.forEach(System.out::println);
            for (Shop shop : list) {
                shopService.add(shop);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 此接口用于接收页面人工审核驳回审核的请求 存入驳回过程信息
     * shop_id,note 并更改state
     * @param shopLog @RequestBody
     * @return JsonResult
     */
    @PostMapping("/audit/reject")
    public JsonResult reject(@RequestBody ShopLog shopLog){
        try {
            shopService.reject(shopLog);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败");
        }
    }

    /** /audit/pass
     * 此接口用于接收页面人工审核通过审核的请求 存入通过过程信息
     * shop_id,note 并更改state
     * @param shopLog @RequestBody
     * @return JsonResult
     */
    @PostMapping("/audit/pass")
    public JsonResult pass(@RequestBody ShopLog shopLog){
        try {
            shopService.pass(shopLog);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }
    }



    /**
     * 此接口用于店铺入驻，根据页面传入的shop对象，存入shop对象以及对应的emp对象
     * @param shop 获取参数方法：@RequestBody
     * @return 返回JsonResult
     */
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {//成功时返回默认JsonResult对象，失败返回调用setMsg方法后的对象
            shopService.settlement(shop);
            return JsonResult.me();//
        } catch (BusinessException e) {
            e.printStackTrace();
            //自定异常
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            //非客户方问题
            return JsonResult.me().setMsg("系统异常，稍后重试");
        }
    }




    /** GetMapping
     * 动词 GET
     * @return 查询所有
     */
    //@RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping()//等同于以上，
    @ApiOperation(value = "查询所有" )
    public List<Shop> findAll(){
        return shopService.findAll();
    }



    /** DeleteMapping PathVariable
     * @return 查询一个
     * @param id
     */
    @GetMapping("/{id}")
    //@PathVariable("{id}" : 从路径中获取id属性
    @ApiOperation(value = "通过ID查询" )
    public Shop findById(@PathVariable("id") Long id){
        return  shopService.findById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除一条" )
    public JsonResult delete(@PathVariable("id") Long id){
        try {//成功时返回默认JsonResult对象，失败返回调用setMsg方法后的对象
            shopService.delete(id);
            return JsonResult.me();//
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");//
        }

    }

    @PatchMapping
    @ApiOperation(value = "通过传入id数组批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {//成功时返回默认JsonResult对象，失败返回调用setMsg方法后的对象
            //System.out.println(ids);
            shopService.patchDel(ids);
            return JsonResult.me();//
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");//
        }

    }



    /**PutMapping RequestBody
     *
     * @param shop
     * @return  判断id是否有值，进行添加或修改操作，返回成功或失败
     */
    @PutMapping()//此处路径里不需要信息，信息再请求体
    //@RequestBody ： 获取请求体的信息
    @ApiOperation(value = "根据id是否有值，添加或修改" )
    public JsonResult save(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                shopService.add(shop);
            }else {
                shopService.update(shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**PostMapping RequestBody
     *
     * @param query
     * @return 根据页面请求体参数来查询分页
     */
    @PostMapping()
    @ApiOperation(value = "通过currentPage 和pageSize 分页查询" )
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        try {//此处try的作用和上面不同，是为了使错误信息不显示在页面
            //shopService.queryPage(query).getData().forEach(System.out::println);
            return shopService.queryPage(query);

        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }



}
